﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using AIPASoft.CommonLibrary.WPF.MVVM.ViewModel;
using LoginManagerLibrary.Interfaces;

namespace LoginManagerLibrary
{
    internal class ResetPasswordViewModel : ValidationViewModel
    {
        private readonly IResetPassword _iResetPassword;
        private string _userName;
        private string _oldPassword;
        private string _password;
        private string _confirmPassword;

        public ResetPasswordViewModel(IResetPassword iResetPassword)
        {
            _iResetPassword = iResetPassword;
            Initialize();
        }

        private void Initialize()
        {
            _loginCommand = new RelayCommand(OnResetButtonClick);
        }

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; OnPropertyChanged(); }
        }

        public string OldPassword
        {
            get { return _oldPassword; }
            set { _oldPassword = value; OnPropertyChanged(); }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value;
                OnPropertyChanged();
            }
        }

        public string ConfirmPassword
        {
            get { return _confirmPassword; }
            set { _confirmPassword = value;
                OnPropertyChanged();
            }
        }

        #region Reset Command Implementation
        private ICommand _resetCommand;
        public ICommand ResetCommand
        {
            get { return _resetCommand; }
            set { _resetCommand = value; }
        }

        private void OnResetButtonClick(object parameter)
        {
            var passwordBox = parameter as PasswordBox;
            if (passwordBox == null)
            {
                return;
            }
            string password = passwordBox.Password;
            string errorMessage = string.Empty;
            if (CanExecuteLoginCommand(password))
            {
                if (!_iResetPassword.ResetPassword(UserName, password, out errorMessage))
                {
                    //TODO: Update UI with validation Error.
                }
            }
        }

        private bool CanExecuteLoginCommand(string password)
        {
            //No Validation Error            //Password should not be empty or null
            var result = string.IsNullOrEmpty(Error) && !string.IsNullOrEmpty(password);
            return result;
        }

        #endregion
    }
}
